/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.web.controller;

import cc.altius.domain.CustomUserDetails;
import cc.altius.domain.User;
import cc.altius.model.Message;
import cc.altius.service.MessageService;
import cc.altius.service.UserService;
import cc.altius.utils.DateUtils;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

/**
 *
 * @author mudassar
 */
@Controller
@SessionAttributes({"unreadCount"})
public class MessageController {
    
    @Autowired
    private UserService userService;
    @Autowired
    private MessageService messageService;
    
    @ModelAttribute("userList")
    List<User> getUserList() {
        List<User> userList = this.userService.getUserList();
        return userList;
    }
    
    @RequestMapping(value = "/message/sendMessageToAgent.htm", method = RequestMethod.GET)
    String showGetPage(ModelMap modelMap) {
        Message message = new Message();
        modelMap.addAttribute("message", message);
        return "message/sendMessageToAgent";
    }
    
    @RequestMapping(value = "/message/sendMessageToAgent.htm", method = RequestMethod.POST)
    String showGetPage(@ModelAttribute("message") Message message, @RequestParam(value = "selectedCustomerIds", required = true) String selectedCustomerIds, ModelMap modelMap) {
        System.out.println("inside post");
        System.out.println("Subject: " + message.getMessageSubject());
        System.out.println("Message: " + message.getMessageBody());
        System.out.println("selectedCustomerIds :" + selectedCustomerIds);
        int totalMsg = this.messageService.addMessage(message, selectedCustomerIds);
        return "redirect:/message/sentSuccessful.htm?totalMsg=" + totalMsg + "&msg=msg.messageAddedSuccessfully";
    }
    
    @RequestMapping(value = "/message/sentSuccessful.htm", method = RequestMethod.GET)
    String showGetPage(@RequestParam("totalMsg") int totalSent, ModelMap modelMap) {
        
        modelMap.addAttribute("totalSent", totalSent);
        return "message/sentSuccessful";
    }
    
    @RequestMapping(value = "/message/showMessageToAgent.htm")
    String showGetPageToAgent(@RequestParam(value = "startDate", required = true, defaultValue = "0") String startDate,
            @RequestParam(value = "stopDate", required = true, defaultValue = "0") String stopDate,
            @RequestParam(value = "sentBy", required = true, defaultValue = "0") int sentBy,
            ModelMap modelMap,HttpServletRequest request) {
        int page = ServletRequestUtils.getIntParameter(request, "p", 0);
        if (startDate.equals("0")) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calReturn = Calendar.getInstance();
            calReturn.add(Calendar.DATE, -7);
            Date startDate1 = calReturn.getTime();
            startDate = dateFormat.format(startDate1);
        }
        if (stopDate.equals("0")) {
            stopDate = DateUtils.getCurrentDateString(DateUtils.IST, DateUtils.YMD);
            System.out.println(" stopDate :" + stopDate);
        }
        int curUser = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
        List<Map<String, Object>> msgList = this.messageService.getMessageList(curUser, startDate + " 00:00:00", stopDate + " 23:59:59", sentBy);
        //PagedListHolder<Map<String, Object>> msgList = new PagedListHolder<Map<String, Object>>();
        
        //msgList.setSource(msssgList);
        //msgList.setPageSize(10);
        //int items=msgList
        //int pageCount = msgList.getPageCount();
        int msgCount = this.messageService.getMessageCountByUserId(curUser);
        int unreadCount = this.messageService.getunReadMessageCountForCurUser(curUser);
        //modelMap.addAttribute("pageCount",pageCount);
        modelMap.addAttribute("sentBy", sentBy);
        modelMap.addAttribute("unreadCount", unreadCount);
        modelMap.addAttribute("msgCount", msgCount);
        modelMap.addAttribute("msgList", msgList);
        modelMap.addAttribute("startDate", startDate);
        modelMap.addAttribute("stopDate", stopDate);
        return "message/showMessageToAgent";
    }
    
    @RequestMapping(value = "/message/showMessageDetails.htm", method = RequestMethod.GET)
    public String showMessageDetailsGetPage(@RequestParam("messageId") String messageId, ModelMap modelMap) {
        int messageId1=Integer.parseInt(messageId);
        Message message = this.messageService.getMessageDetails(messageId1);
        if(message.getReadDate()== null){
            this.messageService.updateStatus(messageId1);
        }
        modelMap.addAttribute("message", message);
        return "message/showMessageDetails";
    }
    
//     @RequestMapping(value = "/message/showMessageToAgent.htm", method = RequestMethod.GET, params = "page")
//    public String showPage(@RequestParam("page") int page, @ModelAttribute("msgList") PagedListHolder<Map<String, Object>> pageListHolder, ModelMap modelMap) {
//        try {
//            int pageCount = pageListHolder.getPageCount();
//            modelMap.put("pageCount", pageCount);
//            pageListHolder.setPage(page);
//           
//            modelMap.put("msgList", pageListHolder);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return "message/showMessageToAgent";
//    }
}
